﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tsanie.OsuDevice.Network;
using Tsanie.OsuDevice.Utils;

namespace Tsanie.OsuDevice {
    public partial class LoginForm : Form {
        public LoginForm() {
            InitializeComponent();
        }

        private void buttonLogin_Click(object sender, EventArgs e) {
            EnabledControls(false);
            Account.Login(textUsername.Text, textPassword.Text, checkAutologin.Checked, (uinfo) => {
                this.Invoke(new MethodInvoker(() => {
                    if (uinfo != null) {
                        if (checkAutologin.Checked)
                            Config.SetValue("Cookies", Config.Cookies);
                        MainForm mainForm = (MainForm)this.Owner;
                        mainForm.EnabledControl(true);
                        this.Close();
                    } else {
                        MessageBox.Show(this, "登录失败！", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        EnabledControls(true);
                    }
                }));
            });
        }

        private void EnabledControls(bool enabled) {
            textUsername.Enabled = enabled;
            textPassword.Enabled = enabled;
            checkAutologin.Enabled = enabled;
            buttonLogin.Enabled = enabled;
            pictureLoading.Visible = !enabled;
        }

        protected override bool ProcessDialogKey(Keys keyData) {
            if (keyData == Keys.Escape) {
                this.Close();
                return false;
            }
            return base.ProcessDialogKey(keyData);
        }
    }
}
